package com.wuzheng.wisgo.sanitation.cloud.controller.person;


import com.wuzheng.wisgo.sanitation.cloud.model.person.param.LoginParam;
import com.wuzheng.wisgo.sanitation.cloud.model.person.pojo.LoginReturnDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.wuzheng.wisgo.sanitation.framework.core.protocol.Result;
import com.wuzheng.wisgo.sanitation.cloud.service.person.UserService;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.person.User;

import java.util.List;


import org.springframework.web.bind.annotation.RestController;
import com.wuzheng.wisgo.sanitation.framework.base.controller.BaseController;

/**
 * @author：hibon
 * @date：Created in 2019-09-22
 * @description：用户信息 控制器
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("/user")
@Api(value = "UserCtrl", tags = "用户信息接口")
public class UserCtrl extends BaseController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "/login")
    @ApiOperation(value = "用户登录")
    public Result<LoginReturnDto> login(
            @RequestBody @Validated LoginParam loginParam,
            BindingResult bindingResult) throws BindException {
        if(bindingResult.hasErrors()){
            throw new BindException(bindingResult);
        }
        return this.success(userService.selectUser(loginParam));
    }

    /**
     * 获取用户信息列表
     */
    @PostMapping(value = "/list")
    @ApiOperation(value = "获取用户信息列表")
    public Result<List<User>> list() {
        return this.success(userService.selectList());
    }

    /**
     * 新增用户信息
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增用户信息")
    public Result<String> add(User user) {
        userService.insert(user);
        return this.success();
    }

    /**
     * 删除用户信息
     */
    @PostMapping(value = "/delete/{id}")
    @ApiOperation(value = "删除用户信息")
    public Result<String> delete(@PathVariable("id") String id) {
        userService.deleteById(id);
        return this.success();
    }

    /**
     * 修改用户信息
     */
    @PostMapping(value = "/update")
    @ApiOperation(value = "修改用户信息")
    public Result<String> update(User user) {
        userService.updateById(user);
        return this.success();
    }

    /**
     * 获取用户信息
     */
    @GetMapping(value = "/get/{id}")
    @ApiOperation(value = "获取用户信息")
    public Result<User> get(@PathVariable("id") String id) {
        return this.success(userService.selectById(id));
    }
}

